<template>
  <view>
    <my-search @click="gotoSeach"></my-search>
    <view class="scroll-view-container">
      <!-- 左侧滚动区域 -->
      <scroll-view class="scroll-view-left" scroll-y="true" :style="{height: leftwidth + 'px'}">
        <block v-for="(item, i) in cateList" :key="item.cat_id">
          <view :class="['scroll-left-item', active === i ? 'active' : '']" @click="activeChange(i)">{{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧滚动区域 -->
      <scroll-view class="scroll-right" scroll-y="true" :style="{height: leftwidth + 'px'}" :scroll-top="scrollTop">
        <view v-for="item in cateList2" :key="item.cat_id">
          <view class="scroll-right-title">/ {{item.cat_name}} /</view>
          <view class="scroll-right-body">
            <view class="scroll-right-body-item" v-for="item2 in item.children" :key="item2.cat_id" @click="toGoodsList(item2)">
              <image :src="item2.cat_icon.replace('dev', 'web')"></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import cartBadge from '@/mixins/cart-badge.js'
  export default {
    mixins: [cartBadge],
    data() {
      return {
        leftwidth: 0,
        cateList: [],
        active: 0,
        cateList2: [],
        scrollTop: 0
      };
    },
    onLoad() {
      const device = uni.getSystemInfoSync()
      this.leftwidth = device.windowHeight - 50
      this.getCateList()
    },
    methods: {
      // 获取左侧列表数据
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.showMsg('二级导航栏数据获取失败！')
        this.cateList = res.message
        this.cateList2 = res.message[0].children
      },

      // 改变 active
      activeChange(i) {
        this.active = i
        this.cateList2 = this.cateList[i].children
        this.scrollTop = this.scrollTop == 0 ? 1 : 0
      },
      
      // 去 goodsList 页面
      toGoodsList(item) {
        uni.navigateTo({
          url: '/subpkg/goods-list/goods-list?cid=' + item.cat_id
        })
      },
      
      // 去搜索页面
      gotoSeach() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .scroll-view-left {
      width: 120px;
      .scroll-left-item {
        line-height: 60px;
        background-color: #f7f7f7;
        text-align: center;
        font-size: 12px;

        &.active {
          background-color: #ffffff;
          position: relative;

          &::before {
            display: block;
            content: '';
            width: 3px;
            height: 30px;
            position: absolute;
            top: 50%;
            left: 0;
            background-color: #C00000;
            transform: translateY(-50%)
          }
        }
      }
    }

    .scroll-right {
       background-color: #fff;
       
      .scroll-right-title {
        font-size: 24rpx;
        font-weight: bold;
        text-align: center;
        padding: 30rpx 0;
      }

      .scroll-right-body {
        display: flex;
        flex-wrap: wrap;

        .scroll-right-body-item {
          width: 33.33%;
          margin-bottom: 20rpx;
          display: flex;
          flex-direction: column;
          justify-content: space-around;
          align-items: center;

          image {
            width: 120rpx;
            height: 120rpx;
          }

          text {
            font-size: 24rpx;
          }
        }
      }

    }
  }
</style>
